﻿'
' This file is part of AnyRip.
' 
' AnyRip is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' AnyRip is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with AnyRip.  If not, see http://www.gnu.org/licenses/gpl.html.
'
Imports System.Diagnostics
Imports System.IO
Imports System.Threading
Imports System.String

Module AnyDVD

    ' Stops the AnyDVD Service
    Public Sub Disable()

        Utils.WriteLine("Attempting to disable AnyDVD.")

        ' Extract AnyTool.exe
        If Not File.Exists(Environment.GetEnvironmentVariable("Temp") & "\AnyTool.exe") Then
            Utils.WriteLine("Extracting AnyTool.")
            File.WriteAllBytes(Environment.GetEnvironmentVariable("Temp") & "\AnyTool.exe", My.Resources.AnyTool)
        End If

        ' Disable AnyDVD
        Utils.WriteLine("Using AnyTool to disable AnyDVD.")
        Dim _Process As New System.Diagnostics.Process()
        _Process.StartInfo.FileName = Environment.GetEnvironmentVariable("Temp") & "\AnyTool.exe"
        _Process.StartInfo.Arguments = "-d -w"
        _Process.StartInfo.UseShellExecute = True
        _Process.StartInfo.CreateNoWindow = True
        _Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        _Process.Start()

        ' Wait for command to finish
        Utils.WriteLine("Waiting for AnyDVD to disable.")
        Do While Not _Process.HasExited
            Thread.Sleep(1000)
        Loop

        ' Wait 2 additonal seconds
        Threading.Thread.Sleep(2000)

    End Sub

    ' Enables the AnyDVD Service
    Public Sub Enable()

        ' Extract AnyTool.exe
        If Not File.Exists(Environment.GetEnvironmentVariable("Temp") & "\AnyTool.exe") Then
            Utils.WriteLine("Extracting AnyTool.")
            File.WriteAllBytes(Environment.GetEnvironmentVariable("Temp") & "\AnyTool.exe", My.Resources.AnyTool)
        End If

        ' Disable AnyDVD
        Dim _Process As New System.Diagnostics.Process()
        _Process.StartInfo.FileName = Environment.GetEnvironmentVariable("Temp") & "\AnyTool.exe"
        _Process.StartInfo.Arguments = "-e -w"
        _Process.StartInfo.UseShellExecute = True
        _Process.StartInfo.CreateNoWindow = True
        _Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        _Process.Start()

        Utils.WriteLine("Waiting for AnyDVD to enable.")
        Do While Not _Process.HasExited
            Thread.Sleep(1000)
        Loop

        ' Wait 2 additonal seconds
        Threading.Thread.Sleep(2000)

    End Sub

    Function RipDisc(ByVal _Drive As String, ByVal _Title As String, ByVal _Path As String) As Boolean

        ' Check for required arguments
        If IsNullOrEmpty(_Drive) Or IsNullOrEmpty(_Title) Or IsNullOrEmpty(_Path) Then
            Return False
        End If

        ' Extract AnyTool.exe
        If Not File.Exists(Environment.GetEnvironmentVariable("Temp") & "\AnyTool2.exe") Then
            Utils.WriteLine("Extracting AnyTool2.")
            File.WriteAllBytes(Environment.GetEnvironmentVariable("Temp") & "\AnyTool2.exe", My.Resources.AnyTool2)
        End If

        ' Kill The Process
        Dim _Process As New System.Diagnostics.Process()
        _Process.StartInfo.FileName = Environment.GetEnvironmentVariable("Temp") & "\AnyTool2.exe"
        _Process.StartInfo.Arguments = "ripiso " & _Drive & " " & Chr(34) & _Path & "\" & _Title & ".iso" & Chr(34)
        _Process.StartInfo.UseShellExecute = True
        _Process.StartInfo.CreateNoWindow = True
        _Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        _Process.Start()

        Utils.WriteLine("Waiting for AnyDVD to rip disc.")

        Do While Not _Process.HasExited
            Thread.Sleep(1000)
        Loop

        ' Return if result
        If File.Exists(_Path & "\" & _Title & ".iso") Then
            Dim _FileInfo As New FileInfo(_Path & "\" & _Title & ".iso")
            Utils.WriteLine("Wrote " & _Path & "\" & _Title & ".iso" & " with size " & _FileInfo.Length / 1024 / 1024 / 1024 & "GB")
            Return True
        Else
            Return False
        End If

    End Function

End Module
